import pandas as pd
import numpy as np
import os
import sys
import model_code.gen_data_old as gd
import model_code.rfg as rfg
import model_code.per_mea as pm
from tensorflow import keras
import tools.mail_tool as mail

sources = ["tongji","vital"]
pre_time = sys.argv[1] #5 10 15
ioh_time = "1"
ob_wins = [5,10,15]

i=1
input_message = "all finished\ninclude:"
pre_time = int(pre_time)
pre_time = pre_time / 5
for source in sources:
    for ob_win in ob_wins:
        ob_win = int(ob_win)

        d_path = source + "/dynamic_normalization/" + ioh_time + "-bt.csv"
        c_path = "config_bt.json"

        static, dynamic, label = gd.gen_data(source, d_path, c_path, pre_time, ob_win)
        static = static.reshape(static.shape[0], static.shape[1], 1, 1)#训练静态数据
        #reshape参数详解：(sample个数，channel个数,长，宽)？

        r = label.sum()
        l = label.shape[0] - r
        r = l / r
        if r < 1:
                r = 1
        l = 1
        cw = {0: l, 1: r}
        print("cw: 1, " + str(r))
        print("count:",i)
        i+=1

        model_path = "models/BT+Static/" + source + "+BT+Static-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"

        model = rfg.create_model_1(static.shape[1:], dynamic.shape[1:], ob_win)

        model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', pm.AUC])

        dynamic_dim = dynamic.reshape(dynamic.shape[0], dynamic.shape[1], dynamic.shape[2], 1)

        history = model.fit([static,dynamic_dim, dynamic], label, epochs=200, batch_size=1024,class_weight=cw,
                                validation_split=0.3, verbose=2,
                                callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='min'),
                                        keras.callbacks.ModelCheckpoint(model_path, monitor='val_loss', save_best_only=True, mode='min', verbose=0)])
        
        input_message += model_path+"\n"
#mail.mail_tool("Server<Remote_Server>","1945156532@qq.com","Running finished",input_message)
